package com.rp.article.serivce.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.rp.article.mapper.ArticleMapper;
import com.rp.article.serivce.ArticlePortalService;
import com.rp.news.common.PagedResult;
import com.rp.news.enums.ArticleReviewStatus;
import com.rp.news.enums.YesOrNo;
import com.rp.news.util.CommonUtil;
import com.rp.pojo.Article;
import com.rp.vo.ArticleDetailVO;
import com.rp.vo.QueryAllListVo;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class ArticlePortalServiceImpl implements ArticlePortalService {

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private CommonUtil commonUtil;

    /**
     * 管理员查询用户的所有文章列表
     * @param vo
     * @return
     */
    @Override
    public PagedResult queryIndexArticleList(QueryAllListVo vo) {
        Example example = new Example(Article.class);
        example.orderBy("publishTime").desc();
        Example.Criteria criteria = example.createCriteria();
        /**
         * isAppoint=即时发布 表示文章已经直接发布,或者定时任务到点发布的
         * isDelete=未删除 只能看到未删除的文章
         * articleStatus=审核通过的,表示只有文章审核通过/机器,人工审核童年过了才能展示
         */
        criteria.andEqualTo("isAppoint", YesOrNo.NO.type);
        criteria.andEqualTo("isDelete",YesOrNo.NO.type);
        criteria.andEqualTo("articleStatus", ArticleReviewStatus.SUCCESS.type);

        if(StrUtil.isNotBlank(vo.getKeyword())){
            criteria.andLike("title","%"+vo.getKeyword()+"%");
        }
        if(vo.getCategory() != null){
            criteria.andEqualTo("categoryId",vo.getCategory());
        }

        PageHelper.startPage(vo.getPage(),vo.getPageSize());
        List<Article> articles = articleMapper.selectByExample(example);
        return commonUtil.setterPageResult(articles,vo.getPage());
    }

    @Override
    public List<Article> queryHotList() {

        Example example = new Example(Article.class);
        example.orderBy("publishTime").desc();
        Example.Criteria criteria = example.createCriteria();

        criteria.andEqualTo("isAppoint", YesOrNo.NO.type);
        criteria.andEqualTo("isDelete",YesOrNo.NO.type);
        criteria.andEqualTo("articleStatus", ArticleReviewStatus.SUCCESS.type);

        PageHelper.startPage(1,5);
        List<Article> articles = articleMapper.selectByExample(example);
        return articles;
    }

    @Override
    public PagedResult queryArticlListOfWriter(String writerId, Integer page, Integer pageSize) {
        Example example =new Example(Article.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("publishUserId",writerId);
        PageHelper.startPage(page,pageSize);
        List<Article> articles = articleMapper.selectByExample(example);
        return setterPageResult(articles,page);    }

    @Override
    public PagedResult queryGoodArticlListOfWriter(String writerId) {
        Example example =new Example(Article.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("publishUserId",writerId);
        PageHelper.startPage(1,5);
        List<Article> articles = articleMapper.selectByExample(example);
        return setterPageResult(articles,1);
    }

    @Override
    public ArticleDetailVO detail(String articleId) {

        Article entity = new Article();
        entity.setId(articleId);
        entity.setIsAppoint(YesOrNo.NO.type);
        entity.setIsDelete(YesOrNo.NO.type);
        entity.setArticleStatus(ArticleReviewStatus.SUCCESS.type);
        Article article = articleMapper.selectOne(entity);
        ArticleDetailVO convert = Convert.convert(ArticleDetailVO.class, article);
        return convert;
    }


    public PagedResult setterPageResult(List<?> list, Integer page){
        PageInfo<?> pageInfo = new PageInfo<>(list);
        PagedResult result = new PagedResult();
        result.setPage(page);
        result.setTotal(pageInfo.getPages());
        result.setRecords(pageInfo.getTotal());
        result.setRows(list);
        return result;
    }
}
